<?php

/**
 * 关务中心操作日志
 */
namespace App\Service\LogService;
use App\Service\AuthService\ManageWsUserAuthService;
use App\Service\Code\Normal;
class YunCangCustomCenterLogService
{
    /**
     * 记录日志
     * @param array $param
     */
    public function addLog(array $param) 
    {
        if( empty($param['token']) ) {
            return Normal::returnCode(Normal::FAIL,"token参数不能为空");
        }
        
        if( empty($param['ip']) ) {
            return Normal::returnCode(Normal::FAIL,"ip参数不能为空");
        }
        
        if( !(!empty($param['content']) && is_string($param['content'])) ) {
            return Normal::returnCode(Normal::FAIL,"content请求报文参数不能为空，或者不是一个string字符串");
        }
        
        if( empty($param['url']) ) {
            return Normal::returnCode(Normal::FAIL,"url接口请求地址不能为空");
        }
        
        $token          = $param['token'];
        try {
            $result         = ManageWsUserAuthService::instance()->checkUserLoginByToken($token);
            $sys_user_id    = $result['data']['id'] ?? 0;
            $sys_user_name  = $result['data']['name'] ?? '';
            $log = [
                'content'       => "关务中心接口发生操作【{$param['content']}】",
                'ip'            => $param['ip'],
                'url'           => $param['url'],
                'sys_user_id'   => $sys_user_id,
                'sys_user_name' => $sys_user_name
            ];
            ManageWsUserAuthService::instance()->addSystemLog($log);
            return Normal::returnCode(Normal::SUC,"接收处理完成");
        } catch ( \Throwable $th) {
            return Normal::returnCode(Normal::FAIL,"接收处理异常【{$th->getMessage()}】");
        }
    }
}